package com.dg11185.nearshop.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.utils.screen.ScreenSize;

/**
 * 该类参考了{@link AlertDialog}的写法，命名和参数都是一样
 * 该类可以自定义layout，但是layout控件的ID需要统一，不然findViewById()会报错
 * @author：Daibo
 * @time：2014-7-17 下午5:40:12
 */
public class MyDialog extends Dialog {

    private Builder builder = null;

    private MyDialog(Context context, Builder builder) {
        super(context, R.style.dialog);
        this.builder = builder;
        try {
            init();
        } catch (Exception e) {
        }
    }

    private MyDialog(Context context, int theme, Builder builder) {
        super(context, theme);
        this.builder = builder;
        try {
            init();
        } catch (Exception e) {
        }
    }

    public void init() {
        if (builder.mLayout == Builder.DEFAULT_VALUE) {
            setTitle(builder.titleTxt);
        } else {
            
            View dialogView = LayoutInflater.from(builder.mContext).inflate(builder.mLayout, null);
            TextView title = (TextView) dialogView.findViewById(R.id.dialog_title);
            title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            TextView message = (TextView) dialogView.findViewById(R.id.dialog_message);
            TextView cancel = (TextView) dialogView.findViewById(R.id.dialog_cancel);
            TextView confirm = (TextView) dialogView.findViewById(R.id.dialog_confirm);

            if (builder.titleTxt != null) {
                title.setText(builder.titleTxt);
            }
            if (builder.messageTxt != null) {
                message.setText(builder.messageTxt);
            }
            if (builder.cancelTxt != null) {
                cancel.setText(builder.cancelTxt);
            }
            if (builder.confirmTxt != null) {
                confirm.setText(builder.confirmTxt);
            }

            if (builder.cancelListener != null) {
                cancel.setOnClickListener(builder.cancelListener);
            }

            if (builder.confirmListener != null) {
                confirm.setOnClickListener(builder.confirmListener);
            }
            setContentView(dialogView);
        }

        if (builder.scale != Builder.DEFAULT_VALUE) {
            WindowManager.LayoutParams lp = this.getWindow().getAttributes();
            int width = ScreenSize.getInstance().screenPxWidth;
            float density = ScreenSize.getInstance().density;

            if (width / density > 600) {
                lp.width = (int) (width * builder.scale * builder.scale);
            } else {
                lp.width = (int) (width * builder.scale);
            }

            this.getWindow().setAttributes(lp);
        }
    }

    /**
     * Builder for {@link MyDialog}
     */
    public static class Builder {

        public static final int DEFAULT_VALUE = -404;

        private Context mContext = null;

        private int mTheme = DEFAULT_VALUE;

        private int mLayout = DEFAULT_VALUE;

        private double scale = DEFAULT_VALUE;

        private CharSequence titleTxt = null;

        private CharSequence messageTxt = null;

        private CharSequence cancelTxt = null;

        private CharSequence confirmTxt = null;

        private android.view.View.OnClickListener cancelListener = null;

        private android.view.View.OnClickListener confirmListener = null;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder(Context context, int theme) {
            mContext = context;
            mTheme = theme;

        }

        /**
         * 自定义Dialog的布局，它包含的控件ID需要遵循以下名称
         * 
         * @title: R.id.dialog_title
         * @message:R.id.dialog_message
         * @cancel: R.id.dialog_cancel
         * @confirm:R.id.dialog_confirm
         */
        public Builder setLayout(int layoutId) {
            this.mLayout = layoutId;
            return this;
        }

        /**
         * 设置Dialog相对于屏幕的宽度，值的范围为 0-1；
         * 
         * @Exception IllegalArgumentException
         */
        public Builder setScale(double scale) {
            if (0 > scale || 1 < scale) {
                throw new IllegalArgumentException("scale needs to be between 0 and 1");
            }
            this.scale = scale;
            return this;
        }

        /**
         * 自定义Dialog标题
         */
        public Builder setTitle(CharSequence titleTxt) {
            this.titleTxt = titleTxt;
            return this;
        }

        /**
         * 自定义提示内容信息
         */
        public Builder setMessage(CharSequence msgTxt) {
            this.messageTxt = msgTxt;
            return this;
        }
        /**
         * 设置确定按钮的回调Listener
         */
        public Builder setPositiveButton(View.OnClickListener clickListener) {
            this.confirmListener = clickListener;
            return this;
        }
        
        /**
         * 设置确定按钮的内容和点击回调Listener
         */
        public Builder setPositiveButton(CharSequence confirmTxt, View.OnClickListener clickListener) {
            this.confirmTxt = confirmTxt;
            this.confirmListener = clickListener;
            return this;
        }
        /**
         * 设置取消按钮回调Listener
         */
        public Builder setNegativeButton(View.OnClickListener clickListener) {
            this.cancelListener = clickListener;
            return this;
        }
        /**
         * 设置取消按钮内容和点击回调Listener
         */
        public Builder setNegativeButton(CharSequence cancelTxt, View.OnClickListener clickListener) {
            this.cancelTxt = cancelTxt;
            this.cancelListener = clickListener;
            return this;
        }

        /** 将builder对象传入MyDialog中创建对象 */
        public MyDialog create() {
            if (mTheme == DEFAULT_VALUE) {
                return new MyDialog(mContext, this);
            } else {
                return new MyDialog(mContext, mTheme, this);
            }
        }
    }
}
